package org.zky.modules.account.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zky.modules.account.domain.ZkyRoleTab;
import org.zky.modules.account.repository.ZkyRoleTabRepository;
import org.zky.modules.account.service.ZkyRoleTabService;
import org.zky.modules.account.service.dto.ZkyRoleTabDto;
import org.zky.modules.account.service.dto.ZkyRoleTabQueryCriteria;
import org.zky.modules.account.service.mapper.ZkyRoleTabMapper;
import org.zky.utils.FileUtil;
import org.zky.utils.PageUtil;
import org.zky.utils.QueryHelp;
import org.zky.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @author chenglong
* @date 2020-03-23
*/
@Service
//@CacheConfig(cacheNames = "zkyRoleTab")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ZkyRoleTabServiceImpl implements ZkyRoleTabService {

    private final ZkyRoleTabRepository zkyRoleTabRepository;

    private final ZkyRoleTabMapper zkyRoleTabMapper;

    public ZkyRoleTabServiceImpl(ZkyRoleTabRepository zkyRoleTabRepository, ZkyRoleTabMapper zkyRoleTabMapper) {
        this.zkyRoleTabRepository = zkyRoleTabRepository;
        this.zkyRoleTabMapper = zkyRoleTabMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ZkyRoleTabQueryCriteria criteria, Pageable pageable){
        Page<ZkyRoleTab> page = zkyRoleTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zkyRoleTabMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ZkyRoleTabDto> queryAll(ZkyRoleTabQueryCriteria criteria){
        return zkyRoleTabMapper.toDto(zkyRoleTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ZkyRoleTabDto findById(Long roleId) {
        ZkyRoleTab zkyRoleTab = zkyRoleTabRepository.findById(roleId).orElseGet(ZkyRoleTab::new);
        ValidationUtil.isNull(zkyRoleTab.getRoleId(),"ZkyRoleTab","roleId",roleId);
        return zkyRoleTabMapper.toDto(zkyRoleTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ZkyRoleTabDto create(ZkyRoleTab resources) {
        return zkyRoleTabMapper.toDto(zkyRoleTabRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ZkyRoleTab resources) {
        ZkyRoleTab zkyRoleTab = zkyRoleTabRepository.findById(resources.getRoleId()).orElseGet(ZkyRoleTab::new);
        ValidationUtil.isNull( zkyRoleTab.getRoleId(),"ZkyRoleTab","id",resources.getRoleId());
        zkyRoleTab.copy(resources);
        zkyRoleTabRepository.save(zkyRoleTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long roleId : ids) {
            zkyRoleTabRepository.deleteById(roleId);
        }
    }

    @Override
    public void download(List<ZkyRoleTabDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZkyRoleTabDto zkyRoleTab : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" roleName",  zkyRoleTab.getRoleName());
            map.put("role description", zkyRoleTab.getDescription());
            map.put(" menuId",  zkyRoleTab.getMenuId());
            map.put("1:active 2:inactive", zkyRoleTab.getStatus());
            map.put("delete time", zkyRoleTab.getDeleteAt());
            map.put(" createTime",  zkyRoleTab.getCreateTime());
            map.put(" updateTime",  zkyRoleTab.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}